package cn.wolfcode.web.controller;


import cn.wolfcode.domain.MessageBoardDzq;
import cn.wolfcode.domain.MessageReplyDzq;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardDzqService;
import cn.wolfcode.service.IMessageReplyDzqService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/messageBoardDzq")
public class MessageBoardDzqController {

    @Autowired
    private IMessageBoardDzqService messageBoardDzqService;

    @Autowired
    private IMessageReplyDzqService messageReplyDzqService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


//    @RequiredPermission(name="留言板页面",expression="messageBoard:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoardDzq> pageInfo = messageBoardDzqService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
//        业务大类
        model.addAttribute("sys",systemDictionaryItemService.listAll(1L));
        return "messageBoard/list";
    }


//    @RequiredPermission(name="留言板删除",expression="messageBoard:delete")
//    详情页面
    @RequestMapping("/details")
    public String details(Long id,Model model) {
//        获取当前用户留言
        MessageBoardDzq messageBoard = messageBoardDzqService.get(id);
        model.addAttribute("user",messageBoard);
//        回复条数
        int count=messageReplyDzqService.selectCount(id);
        model.addAttribute("counts",count);
//        回复的内容
        List<MessageReplyDzq> contexts= messageReplyDzqService.selectContext(id);
        model.addAttribute("list",contexts);
        return "messageBoard/details";
    }

//    @RequiredPermission(name="留言板删除",expression="messageBoard:delete")
    @RequestMapping("/cateItems")
    @ResponseBody
    public List<SystemDictionaryItem> getcateItems(Long id) {
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.listparent(id);
        return systemDictionaryItems;
    }




    @RequiredPermission(name="留言板删除",expression="messageBoard:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           messageBoardDzqService.delete(id);
        }
        return "redirect:/messageBoardDzq/list";
    }




//    @RequiredPermission(name="留言板新增/编辑",expression="messageBoard:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoardDzq messageBoard) {
        if (messageBoard.getId() == null) {
            messageBoardDzqService.save(messageBoard);
        } else {
            messageBoardDzqService.update(messageBoard);
        }
        return "redirect:/messageBoardDzq/list";
    }


    @RequiredPermission(name="留言板新增/编辑",expression="messageBoard:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("messageBoard",messageBoardDzqService.get(id));
        }
        return "messageBoard/input";
    }
}
